package com.ymatou.iapi.godmch.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createCSBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.createCSCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.cs_info;
import com.ymttest.database.model.godmch.buyer_invite_code;
import com.ymttest.database.model.godmch.buyer_invite_codeExample;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.database.sqlwapper.godmch.buyer_invite_codeWapper;
/**
*新增客服
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_createCS{
	private static createCSBean createcsBean;
	private static createCSCall createcsCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("新增客服");
	}

	@Before
	public void caseUp() {
		createcsBean=new createCSBean();
		createcsCall=new createCSCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createcsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	 private void check_invitecode() throws InterruptedException {

		buyer_invite_codeExample example=new buyer_invite_codeExample();
		example.createCriteria().andUserIdEqualTo(createcsBean.getCsId());
		buyer_invite_codeWapper codeMapper=new buyer_invite_codeWapper();
		List<buyer_invite_code> codes=codeMapper.selectByExample(example);	
		Logger.verifyEquals(1, codes.size(), "验证邀请码表新增一条数据");
		Logger.verifyEquals(2, codes.get(0).getUserType(), "验证UserType=2");
		Logger.verifyEquals(0, codes.get(0).getCodeStatus(), "验证CodeStatus=0");

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_001() {
		Logger.start(true,"新增客服,成功");
		try {
			Integer templateId=1;
			Integer mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
			cs_info csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			Logger.verifyEquals(0, csi.getState(), "验证State");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("create_cs-"+mchId+"-"+csId,"cs_add");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(mchId.toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
			Logger.verifyEquals(csId.toString(), joa.get(0).get("csId").toString(), "验证总线消息csId");
			
			check_invitecode();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_001_1() {
		Logger.start(true,"新增客服,成功,一个商户两个客服");
		try {
			Integer templateId=1;
			Integer mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
			cs_info csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			Logger.verifyEquals(0, csi.getState(), "验证State");
			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("create_cs-"+mchId+"-"+csId,"cs_add");
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(mchId.toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
			Logger.verifyEquals(csId.toString(), joa.get(0).get("csId").toString(), "验证总线消息csId");
			
			check_invitecode();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_002() {
		Logger.start(true,"新增客服,csid输入渠道商id,失败");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
//	        GodmchManageCallService.InitcreateagentBean(csId)
			createcsBean.setCsId(mchId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("5013", createcsCall.getString("status"), "验证status");
			//"msg":"客服只能是单一角色，不能同时为代理或渠道商或者是直销商
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_003() {
		Logger.start(true,"新增客服,csid输入代理商id,失败");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
			
			UserCreateBean usercreateBean2= DGUserCallService.initSellerUsercreateBean();
			Integer agent=DGUserCallService.UserCreateCall(usercreateBean2);
			
			List<createmchBean> createmchbeans=GodmchManageCallService.InitcreatemchBeanForAgent(mchId,agent);
			GodmchManageCallService.createmchCallForAgent(createmchbeans.get(0));
			Logger.comment("创建代理商用户:"+agent);
			
			createcsBean.setCsId(agent);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("5013", createcsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_004() {
		Logger.start(true,"新增客服,csid已经是其他人的客服失败");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
			cs_info csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			Logger.verifyEquals(0, csi.getState(), "验证State");
			
			Logger.comment("csid已经是其他人的客服");
			int mchId2=GodmchManageCallService.CreateSellerMch();
			createcsBean.setMchId(mchId2);
			createcsCall.callService();			
			Logger.verifyEquals("5012", createcsCall.getString("status"), "验证status");
			//"已经是客服身份，不能重复注册
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_006() {
		Logger.start(true,"新增客服,重复调用失败");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();		
			createcsCall.callService();	
			Logger.verifyEquals("5012", createcsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_007() {
		Logger.start(true,"新增客服,验证权限模板id不存在,返回错误");
		try {
			Integer templateId=-1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();		
			createcsCall.callService();	
			Logger.verifyEquals("5022", createcsCall.getString("status"), "验证status");
			//"客服角色不存在"
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_008() {
		Logger.start(true,"被删除的客服再重新成为商家的客服成功(此接口目前没有流量,不使用了)");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
			cs_info csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			Logger.verifyEquals(0, csi.getState(), "验证State");
			GodmchManageCallService.RemoveCs(mchId, csId);
			createcsCall.callService();			
			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
			csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			
			check_invitecode();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_009() {
		Logger.start(true,"被删除的客服再重新成为其他商家的客服成功(此接口目前没有流量,不使用了)");
		try {
			Integer templateId=1;
			int mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
			cs_info csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			Logger.verifyEquals(0, csi.getState(), "验证State");
			GodmchManageCallService.RemoveCs(mchId, csId);
			
			int mchId2=GodmchManageCallService.CreateSellerMch();
	        
	        createcsCall=new createCSCall();
	        createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId2);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
			csi = new DefGodMchWapper().selectCsInfo(mchId2, csId).get(0);
			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
			
			check_invitecode();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_010() {
		Logger.start(true,"新增客服,验证商户id为空,返回错误");
		try {
			Integer templateId=1;
//			UserCreateBean usercreateBean1= DGUserCallService.initSellerUsercreateBean();
//			Integer mchId=DGUserCallService.UserCreateCall(usercreateBean1);
//			Logger.comment("创建渠道商用户:"+mchId);
//			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(mchId);
//			createmchbean.setNickName(usercreateBean1.getNickname());
//			GodmchManageCallService.createmchCallForChannel(createmchbean);
//	        Logger.comment("根据用户创建渠道商:"+mchId);
	        UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(null);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();	
			Logger.verifyEquals("4001", createcsCall.getString("status"), "验证status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_011() {
		Logger.start(true,"新增客服,验证客服id为空,返回错误");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
//			createcsBean.setCsId(csId);
			createcsBean.setMchId(1);
			createcsBean.setTemplateId(1);
			createcsCall.setData(createcsBean);
			createcsCall.callService();	
			Logger.verifyEquals("4001", createcsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_012() {
		Logger.start(true,"新增客服,验证权限模板id为空,返回错误");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(1);
			createcsBean.setMchId(1);
//			createcsBean.setTemplateId(1);
			createcsCall.setData(createcsBean);
			createcsCall.callService();	
			Logger.verifyEquals("4001", createcsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_013() {
		Logger.start(true,"新增客服,验证商户id不存在,返回错误，商户不存在");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(123131231);
			createcsBean.setTemplateId(1);
			createcsCall.setData(createcsBean);
			createcsCall.callService();	
			Logger.verifyEquals("5004", createcsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_014() {
		Logger.start(true,"新增客服,验证商户id为空,返回错误，商户不能为空");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(null);
			createcsBean.setTemplateId(1);
			createcsCall.setData(createcsBean);
			createcsCall.callService();	
			Logger.verifyEquals("4001", createcsCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createCS_015() {
		Logger.start(true,"新增客服,成功");
		try {
			Integer templateId=301;
			Integer mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
			createcsBean.setCsId(csId);
			createcsBean.setMchId(mchId);
			createcsBean.setTemplateId(templateId);
			createcsCall.setData(createcsBean);
			createcsCall.callService();			
//			Logger.verifyEquals("200", createcsCall.getString("status"), "验证status");
//			cs_info csi = new DefGodMchWapper().selectCsInfo(mchId, csId).get(0);
//			Logger.verifyEquals(createcsBean.getCsId(), csi.getCsId(), "验证csId");
//			Logger.verifyEquals(createcsBean.getMchId(), csi.getMchId(), "验证mchId");
//			Logger.verifyEquals(createcsBean.getTemplateId(), csi.getTempldateId(), "验证TemplateId");
//			Logger.verifyEquals(0, csi.getState(), "验证State");
//			MessageListBean MessageListBean = MqQueryCallService.initGodmchManage("create_cs-"+mchId+"-"+csId,"cs_add");
//			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			if(joa.size()==0){
//				Thread.sleep(5000);
//				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
//			}
//			joa.forEach(ele->{
//				Logger.debug(ele.toString());
//			});
//			Logger.verifyEquals(mchId.toString(), joa.get(0).get("mchId").toString(), "验证总线消息mchId");
//			Logger.verifyEquals(csId.toString(), joa.get(0).get("csId").toString(), "验证总线消息csId");
//			
//			check_invitecode();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}